import * as api from '../services/example';

export default {
  namespace: 'product',
  state: {
    productList: [
      // {name: '豆子'},
      // {name: '玉米'},
      // {name: '小麦'}
    ]
  },
  reducers: {
    updateList(state, action) {
      let currentProductList = deepClone(state);
      // currentProductList.productList.push(action.payload);
      currentProductList.productList = action.payload;
      return currentProductList;
    }
  },
  // 异步方法放在这里
  effects: {
    *updateListAsync({ payload }, { call, put }) {  // eslint-disable-line
      yield put({
        type: 'updateList',
        payload
      });
    },
    *updateListHttp({ payload }, { call, put }) {
      const result = yield call(api.getApiUsers, payload);
      const data = result.data;
      if (data) {
        yield put({
          type: 'updateList',
          payload: data
        })
      }
    },
    *getOrganization({ payload }, { call, put }) {
      const result = yield call(api.getOrganization, payload);
      let data = [];
      data.push(result.data);
      console.log(payload, data);
      if (data) {
        yield put({
          type: 'updateList',
          payload: data
        })
      }
    },
  },
}

function deepClone(arr) {
  return JSON.parse(JSON.stringify(arr))
}
